# Last updated: 1 June 2024
# Last updated by: Nazita Lajevardi
# Tested on R 4.3.0

#############################
####  Conjoint analysis #####
###### Data Analysis  #######
#############################



# 1. Clear data and set working directory to your own directory. #####################################
rm(list=ls())
setwd("")
# Nazita personal directory: 
# setwd("/Users/nazita/Dropbox/Personal/MSU/Projects/Debbi Naz Per Conjoint/PS Submission/Replication Files/Conjoint Analysis in R/")
getwd()


# 2. Checking that we have the necessary packages. #############################

check.packages <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}
packages<-c("haven","knitr", "RColorBrewer","effects","cjoint"  ,"nnet","tidyverse",  "stargazer", "broom", "MASS", "xtable", "grid","dplyr", "magrittr")
check.packages(packages)


# 3. loading the packages ######################################################

library(haven)
library(knitr)
library(effects)
library(nnet) #multinom
library(tidyverse)
library(stargazer)
library(broom)
library(MASS)
library(xtable)
library(grid)
library(dplyr)
library(magrittr)
library(cjoint)
library(RColorBrewer)


# 4. loading the data. #########################################################
load("conjointdata.Rdata")


##########################################################
############### IN TEXT TABLES AND FIGURES ###############
##########################################################

################
### Figure 1 ###
################

#Figure 1a: Profile Selection

# AMCE for whether the refugee was chosen. DV1. 
summary(visa<-amce(Y1~ Origin + `Refugee Cause` + Children+ `Language skills`+
                     Gender +Age + Religion,
                   data=conjointdata,na.ignore = TRUE, 
                   baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))


plot(visa, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure 1b: Integration Prospects 

# AMCE for whether the refugee was judged as most likely to integrate successfully. DV2.
summary(integrate<-amce(Y2~Origin +`Refugee Cause`+Children+`Language skills`+
                          Gender+Age+Religion,
                        data=conjointdata,na.ignore = TRUE, 
                        baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

plot(integrate, xlab="Expected Difference in Integration Prospects",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




################
### Figure 2 ###
################


#Figure 2a: Profile Selection Among Young Americans

# AMCE for whether the refugee was chosen. DV1.
summary(visa21<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, age_young==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))




# Create plot
plot(visa21, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure 2b: Profile Selection Among Older Americans

# AMCE for whether the refugee was chosen. DV1.
summary(visa22<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, age_older==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

#Create plot
plot(visa22, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




################
### Figure 3 ###
################


#Figure 3a: Profile Selection Among Democrats

# AMCE for whether the refugee was chosen. DV1.
summary(visa31<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, democrat==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa31, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure 3b: Profile Selection Among Republicans

# AMCE for whether the refugee was chosen. DV1.
summary(visa32<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, republican==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot
plot(visa32, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




################
### Figure 4 ###
################


#Figure 4a: Profile Selection Among High Climate Anxiety Respondents

# AMCE for whether the refugee was chosen. DV1.
summary(visa41<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, anxiety_cc_high==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot 
plot(visa41, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure 4b: Profile Selection Among Low Climate Anxiety Respondents

# AMCE for whether the refugee was chosen. DV1.
summary(visa42<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, anxiety_cc_low==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot 
plot(visa42, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


##############################################
##############################################
############# Appendix Materials #############
##############################################
##############################################

#########%######
#  Figure A1   #
##########%#####

# screenshot from Qualtrics survey


###############
#  Table A1   #
###############

# summary statistics: see stata do file


###############
#  Table A2   #
###############

# our study design


###############
#  Table A3   #
###############

# Bonferroni corrections

# Figure 1a
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa)$amce[summary(visa)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 1b
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(integrate)$amce[summary(integrate)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 2a
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa21)$amce[summary(visa21)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 2b
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa22)$amce[summary(visa22)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 3a
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa31)$amce[summary(visa31)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 3b
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa32)$amce[summary(visa32)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 4a
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa41)$amce[summary(visa41)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars

# Figure 4b
new.sig <- 0.1/17 # alpha / no. of predictors
sig.vars <-summary(visa42)$amce[summary(visa42)$amce$`Pr(>|z|)` < new.sig,] #
sig.vars


#########%######
#  Figure A2   #
##########%#####

#Figure A2a: Profile Selection Among Immigrant Respondents

summary(visa_a2a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, immigrant==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot      
plot(visa_a2a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#Figure A2b: Profile Selection Among Native Born Respondents

# AMCE for whether the refugee was chosen. DV1. 
summary(visa_a2b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, native_born==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a2b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




#########%######
#  Figure A3   #
##########%#####

#Figure A3a: Profile Selection Among High Immigration FT Respondents

# AMCE for whether the refugee was chosen. DV1. 
summary(visa_a3a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, FT_immigrants_high==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot    
plot(visa_a3a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#Figure A3b: Profile Selection Among Low Immigration FT Respondents
summary(visa_a3b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, FT_immigrants_low==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot
plot(visa_a3b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))





#########%######
#  Figure A4   #
##########%#####

#Figure A4a: Profile Selection Among High Climate Participation Respondents
summary(visa_a4a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, high_pp_cc==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot 
plot(visa_a4a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




#Figure A4b: Profile Selection Among Low Climate Participation Respondents

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a4b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, low_pp_cc==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot 
plot(visa_a4b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




#########%######
#  Figure A5   #
##########%#####

#Figure A5a: Profile Selection Among Women
summary(visa_a5a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, female==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a5a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#Figure A5b: Profile Selection Among Men

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a5b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, male==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot
plot(visa_a5b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




#########%######
#  Figure A6   #
##########%#####

#Figure A6a: Profile Selection Among Liberals
summary(visa_a6a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, liberal==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot 
plot(visa_a6a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))





#Figure A6b: Profile Selection Among Conservatives

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a6b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, conservative==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot
plot(visa_a6b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))







#########%######
#  Figure A7   #
##########%#####

#Figure A7a: Profile Selection Among Whites

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a7a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion, 
                     data=subset(conjointdata, white==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a7a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#Figure A7b: Profile Selection Among Non-Whites

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a7b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                       Gender+Age+Religion,
                     data=subset(conjointdata, nonwhite==1),na.ignore = TRUE, 
                     baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a7b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))




#########%######
#  Figure A8   #
##########%#####

#Figure A8a: Profile Selection Among High Education

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a8a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, high_education==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a8a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure A8b: Profile Selection Among Low Education

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a8b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, low_education==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a8b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#########%######
#  Figure A9   #
##########%#####

#Figure A9a: Profile Selection Among High Income

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a9a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, high_income==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a9a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))


#Figure A9b: Profile Selection Among Low Income

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a9b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, low_income==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a9b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))





#########%#######
#  Figure A10   #
##########%######

#Figure A10a: Profile Selection Among Biden Supporters

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a10a<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, biden2020support==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))
# Create plot
plot(visa_a10a, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))



#Figure A10b: Profile Selection Among Trump Supporters

# AMCE for whether the refugee was chosen. DV1.
summary(visa_a10b<-amce(Y1~Origin +`Refugee Cause`+Children+`Language skills`+
                        Gender+Age+Religion,
                      data=subset(conjointdata, trump2020support==1),na.ignore = TRUE, 
                      baselines = baselines, cluster=TRUE, respondent.id = "X_recordId"))

# Create plot
plot(visa_a10b, xlab="Expected Change in Migrant Profile Selection",
     text.size=10, colors =c("#CDCDCD", "#D9D9D9", "#BDBDBD", "#969696", "#737373", "#525252", "#252525"))






#########%#####
#  Table A4   #
###############

# Open-Ended Descriptives: See Stata Do File





#########%#####
#  Table A5   #
###############

# Regression Results: See Stata Do File


